<template>
	<view>
		<view class="Con">
			<image src="/static/yq.png" class="BG" mode="widthFix"></image>
			<view class="Contont">
				{{code}}
				<image :src="iiurl" v-if="iiurl" @longpress="save"
					style="width: 344rpx;height: 344rpx;margin-top: 108rpx;margin-bottom: 93rpx;" mode=""></image>
				<view class="BTN" @click="copyUrl">
					立即邀请
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		h5Url
	} from '@/config/config.js'
	// #ifdef MP-TOUTIAO
	import {
		ttPlatform
	} from '@/config/config.js'
	// #endif
	export default {
		data() {
			return {
				code: '',
				money: 0,
				number: 0,
				is_superior: false,
				inviteKey: '',
				imageUrl: '/static/image/share_image.png',
				shareUrl: '/pages/share/jump',
				iiurl: ''
			}
		},
		computed: {
			appTitle() {
				return this.$store.state.config.shop_name;
			}
		},
		onShow() {
			this.getInviteData();
			this.ifwxl()
			this.share()
		},
		methods: {
			// 判断是不是微信浏览器
			share() {
				let data = {
					page: 1, //首页
					type: 2, //海报
				}
				let userToken = this.$db.get('userToken')
				if (userToken) {
					data.token = userToken
				}
				data.client = 1;
				data.url = h5Url + 'pages/share/jump'
				this.$api.share(data, res => {
					this.iiurl = res.data
				})
			},
			ifwxl() {
				this.ifwx = this.$common.isWeiXinBrowser()
			},
			save() {
				// 将 res.path 作为 filePath 参数，传递给 saveImageToPhotosAlbum 方法
				uni.saveImageToPhotosAlbum({
					filePath: this.iiurl,
					success() {
						uni.showToast({
							title: '图片保存成功'
						})
					},
					fail(error) {
						console.error('保存失败', error);
						uni.showToast({
							icon: 'none',
							title: '图片保存失败'
						})
					}
				});
			},
			//获取数据
			getInviteData() {
				this.$api.myInvite(res => {
					this.code = res.data.code;
					this.money = res.data.money;
					this.number = res.data.number;
					this.is_superior = res.data.is_superior;
				});
			},
			//去佣金明细
			toMoney() {
				this.$common.navigateTo('../balance/details?status=5');
			},
			//去邀请列表
			toList() {
				this.$common.navigateTo('./list');
			},
			//填写设置要求
			setMyInvite() {
				let data = {
					code: this.inviteKey
				}
				this.$api.setMyInvite(data, res => {
					if (res.status) {
						this.$common.successToShow('邀请码填写成功');
						this.is_superior = true;
					} else {
						this.$common.errorToShow(res.msg);
					}
				});
			},
			// 生成邀请海报
			createPoster() {
				let data = {
					page: 1, //首页
					type: 3, //海报
				}
				let userToken = this.$db.get('userToken')
				if (userToken) {
					data.token = userToken
				}

				// #ifdef H5 || APP-PLUS || APP-PLUS-NVUE
				data.client = 1;
				data.url = h5Url + 'pages/share/jump'

				// #endif
				this.$api.share(data, res => {
					if (res.status) {
						this.$common.navigateTo('/pages/share?poster=' + encodeURIComponent(res.data))
					} else {
						this.$common.errorToShow(res.msg)
					}
				});
			},
			//复制URL链接
			copyUrl() {
				let data = {
					page: 1, //首页
					type: 1, //海报
				}
				let userToken = this.$db.get('userToken')
				if (userToken) {
					data.token = userToken
				}

				// #ifdef H5 || APP-PLUS || APP-PLUS-NVUE
				data.client = 1;
				data.url = h5Url + 'pages/share/jump'
				// #endif

				// #ifdef MP-WEIXIN
				data.client = 2;
				data.url = 'pages/share/jump'
				// #endif

				// #ifdef MP-TOUTIAO
				data.client = 4;
				data.url = '/pages/share/jump'
				// #endif

				// #ifdef MP-ALIPAY
				data.client = 6;
				data.url = '/pages/share/jump'
				// #endif

				let _this = this;
				this.$api.share(data, res => {
					if (res.status) {
						uni.setClipboardData({
							data: res.data,
							success: function(data) {
								_this.$common.successToShow('复制成功');
							},
							fail: function(err) {
								_this.$common.errorToShow('复制分享URL失败');
							}
						})
					} else {
						_this.$common.errorToShow('复制分享URL失败');
					}
				});
			},
			//获取分享URL
			getShareUrl() {
				let data = {
					client: 2,
					url: "/pages/share/jump",
					type: 1,
					page: 1,
				};
				let userToken = this.$db.get('userToken');
				if (userToken && userToken != '') {
					data['token'] = userToken;
				}
				this.$api.share(data, res => {
					this.shareUrl = res.data
				});
			}
		},
		//分享
		onShareAppMessage() {
			return {
				title: this.$store.state.config.share_title,
				// #ifdef MP-ALIPAY
				desc: this.$store.state.config.share_desc,
				// #endif
				imageUrl: this.$store.state.config.share_image,
				path: this.shareUrl
			}
		}
	}
</script>

<style scoped lang="scss">
	.Con {
		position: relative;

		.BG {
			position: absolute;
			top: 0;
			left: 0;
			width: 100vw;
			z-index: -10;
		}

		.Contont {
			position: absolute;
			top: 1077rpx;
			left: 105rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			font-weight: bold;
			color: #F84D4A;
			font-size: 60rpx;
			text-align: center;

			.BTN {
				width: 540rpx;
				height: 86rpx;
				background: #F84C49;
				border-radius: 43rpx;
				color: #fff;
				font-size: 36rpx;
				line-height: 86rpx;
			}
		}
	}
</style>